package com.example.weiting.huishenghuo.com.example.myapplication;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.weiting.huishenghuo.R;
import com.example.weiting.huishenghuo.utils.User;

import java.util.ArrayList;


public class Login extends AppCompatActivity {
    EditText username,password;
    Button login;
    ArrayList<User> users;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        //读取主界面传来的用户信息（代替数据库的作业）
        Intent intent = getIntent();
        Bundle args = intent.getBundleExtra("users");
        users = (ArrayList<User>) args.getSerializable("userList");


        //获取相应的组件
        username = (EditText) findViewById(R.id.userName);
        password = (EditText) findViewById(R.id.password);
        login = (Button) findViewById(R.id.dengLu);

        //判断是否登陆成功
        login.setOnClickListener(new View.OnClickListener() {
                @Override
            public void onClick(View view) {
                int i = 0;
                for (;i < users.size();i++){
                    if (users.get(i).getUsername().equals(username.getText().toString())){
                        if(users.get(i).getPassworld().equals(password.getText().toString())){
                            //页面跳转
                            startActivity(new Intent(Login.this,Record.class));
                            break;
                        }else{
                            Toast.makeText(Login.this, "输入密码有误，请重新输入!", Toast.LENGTH_SHORT).show();
                            break;
                        }
                    }
                }
                if (i == users.size()){
                    Toast.makeText(Login.this, "用户名不存在", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}
